const Controllers = require('../../controllers/Controller.js').controller;
const util = require('../../../utils/util.js');
const controllers = require("../../controllers/controllerIndex").controller

Page({
  data: {
    vipdetailsdata:'',
    vipId:'',
    switch1Checked:false,
    shopImg:"",
    title:"",
    isRead:""
  },
  onLoad: function (options) {
    this.setData({
      shopImg:wx.getStorageSync('shopImg')
    })
    if(options.id){
      this.setData({vipId:options.id})
    }
    if(options.isRead){
      this.setData({
        isRead:options.isRead
      })
    }
  },
  onShow(){
    this.vipdetails(this.data.vipId)
    var chooseProduct=this.data.chooseProduct
    var all=this.data.vipdetailsdata.goods || []
    all.forEach(function(item,index){
      chooseProduct.forEach(function(item1,index1){
        if(item.id==item1.id){
          chooseProduct.splice(index1,1)
        }
      })
    })
    if(chooseProduct){
      for(let item of chooseProduct){
        item.num=1
      }
      this.setData({
        'vipdetailsdata.goods':all.concat(chooseProduct)
      })
    }

  },

  //提前结束活动
  endActivity(e){
    let index=e.currentTarget.dataset.index
    let value={}
    value.shopId=wx.getStorageSync('shopId')
    value.vipId=this.data.vipId
    let that=this

    wx.showModal({
      title: '提示',
      content: '是否提结束？',
      success (res) {
        if (res.confirm) {
          Controllers.setCardOver(value).then(res=>{
            wx.showToast({
              title: '操作成功',
            })
            setTimeout(function(){
              wx.navigateBack()
            },1000)
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },

  //跳转会员卡编辑页
  toSetCard(e){
    if(this.data.vipdetailsdata.type==0||this.data.vipdetailsdata.type==5){
      wx.navigateTo({
        url: '/pages/diyActivity/adddiy/adddiy?id='+this.data.vipdetailsdata.id,
      })
    }else{
      wx.navigateTo({
        url: '/pages/diyActivity/addcard/addcard?id='+this.data.vipdetailsdata.id,
      })
    }
  },

  // 发布会员卡
  pushVipCard(){
    let value={}
    value.vipId=this.data.vipId
    value.shopId=wx.getStorageSync('shopId')
    Controllers.pushVip(value).then(res=>{
      wx.showToast({
        title: '发布成功',
        duration:1000
      })
      setTimeout(function(){
        wx.navigateBack()
      },1000)
    })
  },

  vipdetails(id){
    Controllers.vipdetails({shopId:wx.getStorageSync('shopId'),vipId:id}).then(res=>{
 
          if(res.data.startTime)
          res.data.startTime = res.data.startTime.replace(/-/g, '/')
          if(res.data.endTime)
          res.data.endTime = res.data.endTime.replace(/-/g, '/')
          

      this.setData({
        vipdetailsdata:res.data,
      })

      if(res.data.type==4){
        this.setData({
          title:"引流卡"
        })
      }else{
        this.setData({
          title:res.data.name
        })
      }

      this.getcode()
    })
  },
  switchChange(e){
    var type=e.currentTarget.dataset.type;
    if(type==1){
      if(e.detail.value){
        this.setData({
          'vipdetailsdata.haveGivePrice':1,
          'vipdetailsdata.haveGiveIntegral':0,
          switch1Checked:false
        })
      }else{
        this.setData({
          'vipdetailsdata.haveGivePrice':0,
          'vipdetailsdata.haveGiveIntegral':0,
          switch1Checked:false
        })
      }
    }
    if(type==2){
      if(e.detail.value){
        this.setData({
          'vipdetailsdata.haveGivePrice':0,
          'vipdetailsdata.haveGiveIntegral':1,
          switch1Checked:false
        })
      }else{
        this.setData({
          'vipdetailsdata.haveGivePrice':0,
          'vipdetailsdata.haveGiveIntegral':0,
          switch1Checked:false
        })
      }
    }
    if(type==3){
      if(e.detail.value){
        this.setData({
          'vipdetailsdata.haveGivePrice':0,
          'vipdetailsdata.haveGiveIntegral':0,
          switch1Checked:true
        })
      }else{
        this.setData({
          'vipdetailsdata.haveGivePrice':0,
          'vipdetailsdata.haveGiveIntegral':0,
          switch1Checked:false
        })
      }
    }
  },
  addgoods(e){
    let nowListSting = JSON.stringify(this.data.chooseProduct);
    this.setData({
      'vipdetailsdata.haveGivePrice':0,
      'vipdetailsdata.haveGiveIntegral':0,
      switch1Checked:true
    })
    wx.navigateTo({
      url: '/pages/settlement/AddGoods/AddGoods?nowList=' + nowListSting,
    })
  },
  cancel(e){
    wx.navigateBack({
      delta: 1
    })
  },
  delete(e){
    this.data.vipdetailsdata.goods.splice(e.currentTarget.dataset.index,1)
    this.setData({
      vipdetailsdata:this.data.vipdetailsdata
    })
  },
  plus(e){
    var index=e.currentTarget.dataset.index
    this.data.vipdetailsdata.goods[index].num++;
    this.setData({
     vipdetailsdata:this.data.vipdetailsdata
    })
  },
  mins(e){
    var index=e.currentTarget.dataset.index
    if(this.data.vipdetailsdata.goods[index].num>1){
      this.data.vipdetailsdata.goods[index].num--;
      this.setData({

        vipdetailsdata:this.data.vipdetailsdata
      })
    }
  },
  changenum(e){
    var index=e.currentTarget.dataset.index
    if(this.data.vipdetailsdata.goods[index].num<1){
      this.data.vipdetailsdata.goods[index].num=1
      this.setData({

        vipdetailsdata:this.data.vipdetailsdata
      })
    }else{
      this.data.vipdetailsdata.goods[index].num=e.detail.value
      this.setData({

        vipdetailsdata:this.data.vipdetailsdata
      })
    }
  },
  changeinput(e){
    var name=e.currentTarget.dataset.name;
    var val=e.detail.value;
    this.setData({
        [name]:val || ''
    })
  },
  vipsave(){
    if(this.data.vipdetailsdata.haveGivePrice==1 && !this.data.vipdetailsdata.givePrice){
        wx.showToast({
          title: '请输入赠送金额',
          icon: 'none',
          duration: 2000
        })
        return
    }
    if(this.data.vipdetailsdata.haveGiveIntegral==1 && !this.data.vipdetailsdata.giveIntegral){
        wx.showToast({
          title: '请输入赠送积分',
          icon: 'none',
          duration: 2000
        })
        return
    }
    let data=this.data.vipdetailsdata
    if(data.goods.length>0){
      let goodsIdNum=[]
      for(let item of data.goods){
        goodsIdNum.push(item.id+'-'+item.num);
      }
      data.goodsIdNum=goodsIdNum.join(',')
    }
    if(data.id){
      data.vipId=data.id
      delete data.id
    }
    delete data.goods;
    if(data.image==null || !data.image){
      delete data.image;
    }
    Controllers.vipsave(data).then(res => {
      wx.showToast({
        title: '添加成功！',
        icon: 'success',
        duration: 2000
      })
      setTimeout(res => {
        wx.navigateBack({ //返回
          delta: 1
        })
      },2000)
    });
  },
  deletedetail(){
    let data={shopId:wx.getStorageSync('shopId'),vipId:this.data.vipId}
    Controllers.vipdelete(data).then(res => {
      wx.showToast({
        title: '删除成功！',
        icon: 'success',
        duration: 2000
      })
      setTimeout(res => {
        wx.navigateBack({ //返回
          delta: 1
        })
      },2000)
    });
  },


  getcode(){
    let shopId=wx.getStorageSync('shopId');
    let vipId=this.data.vipdetailsdata.id;
    let name=this.data.vipdetailsdata.name;
    let page="pages/my/vip/info/VIPCardInfo";
    let scene="id:"+vipId+","+"shopId:"+shopId;
    controllers.getCode({shopId:shopId,page:page,scene:scene,name:name}).then(res=>{
      this.setData({
        link:res.data
      })
    })
  },


  toCode(){
    let link=this.data.link;
    link=encodeURIComponent(link)
    wx.navigateTo({
      url: '/pages/Index/QRCode/QRCode?link='+link,
    })
  }
})
